package com.zc.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zc.order.entity.Order;
import com.zc.order.service.OrderService;
import com.zc.utils.JwtUtils;
import com.zc.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author cong
 * @since 2022-12-04
 */
@RestController
@RequestMapping("/order/order")
@CrossOrigin
public class OrderController {

	@Autowired
	private OrderService orderService;

	@PostMapping("/createOrder/{courseId}")
	public R createOrder(@PathVariable String courseId, HttpServletRequest request) {
		String orderNo = orderService.createOrder(courseId, JwtUtils.getMemberIdByJwtToken(request));
		return R.ok().data("orderNo", orderNo);
	}

	@GetMapping("/getOrderInfo/{orderNo}")
	public R getOrderInfo(@PathVariable String orderNo) {
		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		wrapper.eq("order_no", orderNo);
		Order order = orderService.getOne(wrapper);
		return R.ok().data("order", order);
	}

	@GetMapping("/queryCourseIsBuyed/{courseId}/{memberId}")
	public boolean queryCourseIsBuyed(@PathVariable String courseId, @PathVariable String memberId){
		QueryWrapper<Order> wrapper = new QueryWrapper<>();
		wrapper.eq("course_id", courseId)
			   .eq("member_id", memberId)
				.eq("status", 1);
		int count = orderService.count(wrapper);
		return count > 0 ? true : false;
	}

}

